Manage remote VMs with virsh
The objective of this post is to describe the steps needed to manage a VM running on a remote KVM host.
Prerequisites:
- running KVM remote hypervisor
- have a remote VM on the hypervisor
- ssh access to the hypervisor
Using virsh connect parameter
virsh
can be used to manage VMs (and volumes, network, etc...) on the hypervisor. And this can be done when the hypervisor is a remote machine as well.
To achieve this, in the remote client you will run:
apt install libvirt-clients
virsh --connect qemu+ssh://devops@hypervisor:/system
This will connect to a remote host called hypervisor
using ssh. Once you are connected you can do virsh stuff on it.
virsh # list
Id Name State
----------------------------------------------------
1 VM1 running
2 VM2 running
virsh # start VM3
Domain VM3 started
virsh # list
Id Name State
----------------------------------------------------
1 VM1 running
2 VM2 running
3 VM3 running
virsh # quit
Automating the boring stuff
You can define your default hypervisor in libvirt configurations file ~/.local/etc/libvirt/libvirt.conf
echo "XDG_CONFIG_HOME=~/.local/etc/" >> ~/.bashrc
export XDG_CONFIG_HOME=~/.local/etc/
nano ~/.local/etc/libvirt/libvirt.conf
uri_aliases = ["hypervisor=qemu+ssh://devops@hypervisor:/system"]
uri_default = "qemu+ssh://devops@hypervisor:/system"
Once you have defined your defaults you can manage your VMs with:
virsh # to use uri_default
virsh --connect hypervisor # to use uri_aliases
References
- virsh man page
- libvirt official documentation for URIs and remote connections
- Red Hat's excellent libvirt documentation